using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Entities;
using BUS;
using Utility;

public partial class Admin_DealDanhSach : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
            LoadDropDownList();
        }
    }
    protected void LoadDropDownList()
    {
        tbDealCollection dealCollection = tbDealBUS.GetAll();
        ddl_madeal.DataTextField = tbDealColumns.MaDeal.ToString();
        ddl_madeal.DataValueField = tbDealColumns.MaDeal.ToString();
        ddl_madeal.DataSource = dealCollection;
        ddl_madeal.DataBind();
        ddl_madeal.Items.Insert(0, new ListItem("All", "0"));

        tbSanPhamCollection spCollection = tbSanPhamBUS.GetAll();
        ddl_sanpham.DataTextField = tbSanPhamColumns.TenSP.ToString();
        ddl_sanpham.DataValueField = tbSanPhamColumns.MaSP.ToString();
        ddl_sanpham.DataSource = spCollection;
        ddl_sanpham.DataBind();
        ddl_sanpham .Items .Insert (0,new ListItem ("All","0"));

        tbTinhTrangDealCollection ttdCollection = tbTinhTrangDealBUS.GetAll();
        ddl_tinhtrang.DataTextField = tbTinhTrangDealColumns.TenTTDEAL .ToString();
        ddl_tinhtrang.DataValueField = tbTinhTrangDealColumns.MaTTDEAL .ToString();
        ddl_tinhtrang.DataSource = ttdCollection;
        ddl_tinhtrang.DataBind();
        ddl_tinhtrang.Items.Insert(0, new ListItem("All", "0"));
    }
    private void LoadData()
    {
        Pager.PageSize = 5;
        Criteria c = new Criteria();

        if (ddl_madeal.SelectedValue != "0")
        {
            c.add(Condition.AND, tbDealColumns.MaDeal, Condition.LIKE, ddl_madeal.SelectedValue);
        }
        if (ddl_sanpham.SelectedValue != "0")
        {
            c.add(Condition.AND, tbDealColumns.SanPham, Condition.LIKE,ddl_sanpham.SelectedValue);
        }
        if (ddl_tinhtrang.SelectedValue != "0")
        {
            c.add(Condition.AND, tbDealColumns.TinhTrang, Condition.LIKE, ddl_tinhtrang.SelectedValue);
        }

        int total = 0;
        tbDealCollection  dealCollection = tbDealBUS.Get(c.Criter, Pager.CurrentIndex, Pager.PageSize,
        tbDealColumns.MaDeal.ToString(), OrderDirection.ASC.ToString(), out total);
        Repeater1.DataSource = dealCollection;
        Repeater1.DataBind();
        Pager.ItemCount = total;
        if (total <= Pager.PageSize)
        {
            Pager.Visible = false;
        }
        
    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            tbDeal  deal = (tbDeal )e.Item.DataItem;
            Literal lit_MaDeal = (Literal)e.Item.FindControl("lit_MaDeal");
            Literal lit_SanPham = (Literal)e.Item.FindControl("lit_SanPham");
            Literal lit_TinhTrang = (Literal)e.Item.FindControl("lit_TinhTrang");
            HyperLink hpl_Edit = (HyperLink)e.Item.FindControl("hpl_Edit");
            hpl_Edit.NavigateUrl = "~/Admin/DealUpdate.aspx?iD=" + deal.MaDeal.ToString();
            tbSanPham sanpham = tbSanPhamBUS.GetByID(deal.SanPham);
            lit_SanPham.Text = sanpham.TenSP;
            tbTinhTrangDeal  tinhtrangdeal = tbTinhTrangDealBUS.GetByID(deal.TinhTrang);
            lit_TinhTrang.Text = tinhtrangdeal.TenTTDEAL  ;
        }
    }
    protected void btn_Search_Click(object sender, EventArgs e)
    {
        LoadData();
    }

    protected void lbt_Xoa_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < Repeater1.Items.Count; i++)
        {
            CheckBox cb_Xoa = (CheckBox)Repeater1.Items[i].FindControl("cb_Xoa");
            if (cb_Xoa.Checked == true)
            {
                HiddenField hf_id = (HiddenField)Repeater1.Items[i].FindControl("hf_id");
                int iD = int.Parse(hf_id.Value);
                tbDeal  deal = tbDealBUS.GetByID(iD);
                tbDealBUS.Delete(iD);
            }
        }
        Response.Redirect(Request.RawUrl);
    }
    protected void Pager_Command1(object sender, CommandEventArgs e)
    {
        Pager.CurrentIndex = int.Parse(e.CommandArgument.ToString());
        LoadData();


    }
}
